package com.yanqu.road.server.cmd.activity.unionwar;

import com.yanqu.road.entity.activity.ActivityRewardResult;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.unionwar.UnionWarActivityModule;
import com.yanqu.road.server.pb.ActivityPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = GameProtocol.S_UNION_WAR_ACTIVITY_GET_GUESS_REWARD, desc = "获取群英争霸竞猜奖励")
public class UnionWarGetGuessRewardCmd extends AbstractCommand {
    private int activityId;
    private int stage;
    private ActivityRewardResult rewardResult;
    private boolean isNeedResp = true;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionWarActivityServerProto.UnionWarGetGuessRewardMsg msg = UnionWarActivityServerProto.UnionWarGetGuessRewardMsg.parseFrom(packet.getBytes());
        activityId = msg.getActivityId();
        stage = msg.getPhase();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        rewardResult = player.getModule(UnionWarActivityModule.class).getGuessReward(activityId, null, stage, false);
        if (rewardResult.getRet() == 0) {
            isNeedResp = false;
        }
        rewardResult.setActivityId(activityId);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (isNeedResp) {
            ActivityProto.ActivityGetRewardRespMsg.Builder respMsg = ActivityPb.parseActivityGetRewardRespMsg(rewardResult);
            if(null != respMsg) {
                player.sendPacket(Protocol.U_UNION_WAR_ACTIVITY_GET_GUESS_REWARD, respMsg);
            }
        }
    }
}
